//
// Created by Soar Qin on 2017/8/25.
// Copyright (c) 2017 Soar Qin. All rights reserved.
//

#pragma once

#include "io_event.hh"

namespace core::async {

class IPAddr;
struct UDPEndpointContext;

class UDPEndpoint: public IoEvent {
    DISABLE_CLASS_COPY(UDPEndpoint);
public:
    enum {
        PACKET_SIZE_MAX = 1420
    };
    explicit UDPEndpoint(Loop &loop);
    ~UDPEndpoint() override;

    bool bind(const IPAddr &addr);
    bool bind(const std::string &address);
    bool bind(const std::string &ip, uint16_t port);

    inline bool isIPv6() { return isv6_; }

    virtual void onRead(const void *buf, size_t size, const IPAddr &from) = 0;
    virtual void onWritten() {}
    virtual void onClosed() {}
    virtual int write(const void *buf, size_t size, const IPAddr &to);

    bool close() override;

protected:
    void onEvent(short events) override;

protected:
    UDPEndpointContext *udp_;
    bool isv6_ = false;
};

}
